using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region Template

	/// <summary>
	/// Template object for NHibernate mapped table 'Template'.
	/// </summary>
	[Serializable]
	public class Template : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _fileName;
		protected string _filePath;
		protected string _owner;
		protected Nullables.NullableDateTime _lastUpdate;
		protected string _templateForApp;
		protected string _templateOrSample;
		protected DicItem _templateType;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public Template() { }

		public Template( string fileName, string filePath, string owner, Nullables.NullableDateTime lastUpdate, string templateForApp, string templateOrSample, DicItem templateType )
		{
			this._fileName = fileName;
			this._filePath = filePath;
			this._owner = owner;
			this._lastUpdate = lastUpdate;
			this._templateForApp = templateForApp;
			this._templateOrSample = templateOrSample;
			this._templateType = templateType;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string FileName
		{
			get { return _fileName; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for FileName", value, value.ToString());
				_fileName = value;
			}
		}

		public string FilePath
		{
			get { return _filePath; }
			set
			{
				if ( value != null && value.Length > 1000)
					throw new ArgumentOutOfRangeException("Invalid value for FilePath", value, value.ToString());
				_filePath = value;
			}
		}

		public string Owner
		{
			get { return _owner; }
			set
			{
				if ( value != null && value.Length > 250)
					throw new ArgumentOutOfRangeException("Invalid value for Owner", value, value.ToString());
				_owner = value;
			}
		}

		public Nullables.NullableDateTime LastUpdate
		{
			get { return _lastUpdate; }
			set { _lastUpdate = value; }
		}

		public string TemplateForApp
		{
			get { return _templateForApp; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for TemplateForApp", value, value.ToString());
				_templateForApp = value;
			}
		}

		public string TemplateOrSample
		{
			get { return _templateOrSample; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for TemplateOrSample", value, value.ToString());
				_templateOrSample = value;
			}
		}

		public DicItem TemplateType
		{
			get { return _templateType; }
			set { _templateType = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is Template))
				throw new InvalidCastException("This object is not of type Template");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((Template)obj).Id);
					break;
				case "FileName":
					relativeValue = this.FileName.CompareTo(((Template)obj).FileName);
					break;
				case "FilePath":
					relativeValue = this.FilePath.CompareTo(((Template)obj).FilePath);
					break;
				case "Owner":
						relativeValue = ( this.Owner != null ) ? this.Owner.CompareTo(((Template)obj).Owner) : -1;
					break;
				case "LastUpdate":
					relativeValue = this.LastUpdate.CompareTo(((Template)obj).LastUpdate);
					break;
				case "TemplateForApp":
						relativeValue = ( this.TemplateForApp != null ) ? this.TemplateForApp.CompareTo(((Template)obj).TemplateForApp) : -1;
					break;
				case "TemplateOrSample":
						relativeValue = ( this.TemplateOrSample != null ) ? this.TemplateOrSample.CompareTo(((Template)obj).TemplateOrSample) : -1;
					break;
                default:
                    goto case "Id";
			}
            if (Template.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
